package com.clkj.nangang.http;

import com.google.gson.JsonObject;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.POST;
import retrofit2.http.Query;
import rx.Observable;

/**
 * Created by Administrator on 2017/3/1.
 */

public interface HttpService {


    /**
     * 获取列表
     *
     * @return
     */
    @FormUrlEncoded
    @POST("examlpelist")
    Observable<JsonObject> getExampleList(
    );

    //登陆
    //Tel=>手机号码，Password=>密码，
    @POST("SystemHandler.ashx?Action=PersonsLogin")
    Observable<JsonObject> validateLogin(@Query("MobilePhoneNumber") String MobilePhoneNumber, @Query("Password") String Password);

    //注册
    //Tel=>手机号码，Password=>密码，
    @POST("SystemHandler.ashx?Action=PersonsAdd")
    Observable<JsonObject> register(@Query("Name") String Name, @Query("Password") String Password,
                                    @Query("MobilePhoneNumber") String MobilePhoneNumber, @Query("UrbanRural") String UrbanRural,
                                    @Query("Quanters") String Quanters, @Query("Floor") String Floor, @Query("Room") String Room);

    //获取小区列表或者组列表
    @POST("SystemHandler.ashx?Action=GetDepartment")
    Observable<JsonObject> getQuarters(@Query("IsQOrX") String IsQOrX);

    //获取新闻列表
    @POST("NewsHandler.ashx?Action=getNewsNoCList")
    Observable<JsonObject> getNewsList(@Query("CategoryName") String CategoryName, @Query("Page") String Page);

    //获取新闻详情
    @POST("NewsHandler.ashx?Action=getNewsContent")
    Observable<JsonObject> getNewsDetail(@Query("ID") String ID);

    //获取居家服务
    @POST("NewsHandler.ashx?Action=getFWNewsList")
    Observable<JsonObject> getHomeService(@Query("FWType") String FWType, @Query("Page") String Page);

    //获取干部列表
    @POST("SuperviseHandler.ashx?Action=getSuperviseList")
    Observable<JsonObject> getGunbus(@Query("Page") String Page);

    //提交诉求
    @POST("AskingHandler.ashx?Action=AskingAdd")
    Observable<JsonObject> submitAppeal(@Query("PersonID") String PersonID,@Query("Reason") String Reason);

    class Factory {

        private static OkHttpClient generateOkHttpClient() {
            OkHttpClient httpClient = new OkHttpClient.Builder()
                    .readTimeout(5, TimeUnit.MINUTES)
                    .connectTimeout(5, TimeUnit.MINUTES)
                    .writeTimeout(5, TimeUnit.MINUTES)
                    .addInterceptor(new LoggingInterceptor())
                    .build();


            /*InputStream cerInputStream = null;
            HttpsUtils.SSLParams sslParams = HttpsUtils.getSslSocketFactory(new InputStream[]{cerInputStream},null,null);

            OkHttpClient httpClient = new OkHttpClient.Builder()
                    .readTimeout(5, TimeUnit.MINUTES)
                    .connectTimeout(5, TimeUnit.MINUTES)
                    .writeTimeout(5, TimeUnit.MINUTES)
                    .sslSocketFactory(sslParams.sSLSocketFactory,sslParams.trustManager)
                    .build();*/


            return httpClient;
        }

        public static HttpService create(String baseUrl) {

            /*Gson gson = new GsonBuilder()
                    .setLenient()
                    .create();*/

            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(baseUrl + "/")
                    .client(generateOkHttpClient())
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .build();
            return retrofit.create(HttpService.class);
        }
    }


}
